home *** CD-ROM | disk | FTP | other *** search
/ Computer Inter@ctive 17 / Computer Interactive cdrom 17 - gen 99.iso / ZDNETIT / CONTENT / SMTPCEMS.ZIP / FROM.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-11-15  |  11.2 KB  |  369 lines

  1. /*
  2. **  FROM.C [edit EMAIL.H before compiling]
  3. **
  4. **  This program displays the header from each email message 
  5. **  on the SMTP server. This is the GUI equivalent of the console
  6. **  mode example STATUS.C.
  7. **
  8. **  NOTES:
  9. **    This program disables the automatic calling of the driver.
  10. **    Thus, seeDriver must be called after every call to the SEE
  11. **    function library. This was done to integrate the functioning
  12. **    of SEE with the Windows event loop and to demonstrate the 
  13. **    explicit use of seeDriver.
  14. */
  15.  
  16. #include <windows.h>
  17. #include <winsock.h>
  18.  
  19. #include "see.h"
  20. #include "message.h"
  21. #include "paint.h"
  22. #include "about.h"
  23. #include "str.h"
  24.  
  25. #include "email.h"
  26.  
  27. #ifdef WIN32
  28. #define USE_INS HINSTANCE
  29. #define USE_PTR PSTR
  30. #else
  31. #define USE_INS HANDLE
  32. #define USE_PTR LPSTR
  33. #endif
  34.  
  35. #define POST_MSG(m) PostMessage(hMainWnd,WM_USER,(m),0)
  36. ///#define POST_PARM(m,p) PostMessage(hMainWnd,WM_USER,(m),(p))
  37.  
  38. #define QUOTE 0x22
  39.  
  40. LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);
  41. HWND hMainWnd;                    /* main window handle */
  42.  
  43. #define BUFF_SIZE  2048
  44. #define TEMP_SIZE   128
  45. #define STR_SIZE     50
  46.  
  47. #define CMD_NONE        0
  48. #define CMD_CONNECT     1
  49. #define CMD_GET_COUNT   2
  50. #define CMD_READY       3
  51. #define CMD_NEXT_MSG    4
  52. #define CMD_GET_HEADERS 5
  53. #define CMD_CLOSE       6 
  54. #define CMD_DRIVER      7
  55.  
  56. static USE_INS hInstance;
  57. static int WinWidth = 8 * NCOLS;   /* window width */
  58. static int WinHeight = 15 * NROWS; /* window height */              
  59. static char Temp[TEMP_SIZE];       /* temporary buffer */
  60. static char Buffer[BUFF_SIZE];     /* buffer for headers */ 
  61. static HCURSOR ArrowCursor;        /* arrow cursor */
  62. //static HCURSOR WaitCursor;         /* hour glass cursor */
  63. static int NextCommand = CMD_NONE; /* next command to execute */
  64. static int MessageCount;           /* number of messages */
  65. static int MessageNbr;             /* current message number [1,MessageCount] */
  66. static char Pop3String[STR_SIZE] = POP3_HOST_NAME;  /* POP3 server name*/
  67. static char UserString[STR_SIZE] = POP3_USER_NAME;  /* POP3 user name */
  68. static char PassString[STR_SIZE] = POP3_PASSWORD;   /* POP3 password */       
  69.               
  70. /* display error message */
  71.  
  72. static void DisplayError(int Code, LPSTR Msg)
  73. {DisplayString("ERROR: ");
  74.  if(Msg) DisplayString(Msg);
  75.  if(Code)
  76.    {seeErrorText(Code,(LPSTR)Temp,50);
  77.     DisplayLine((LPSTR)Temp);
  78.    }
  79.  /* restore arrow cursor */
  80.  SetCursor(ArrowCursor);
  81. }
  82.  
  83. /* display email parameters */
  84.  
  85. void ShowINI(void)
  86. {wsprintf((LPSTR)Temp,"  Server : %c%s%c", QUOTE,(LPSTR)Pop3String,QUOTE);
  87.  DisplayLine((LPSTR)Temp);
  88.  wsprintf((LPSTR)Temp,"    User : %c%s%c", QUOTE,(LPSTR)UserString,QUOTE);
  89.  DisplayLine((LPSTR)Temp);
  90.  wsprintf((LPSTR)Temp,"Password : %c%s%c", QUOTE,(LPSTR)PassString,QUOTE);
  91.  DisplayLine((LPSTR)Temp);
  92. }  
  93.  
  94. /* WinMain */
  95.  
  96. #ifdef WIN32
  97. int WINAPI
  98. #else
  99. int PASCAL
  100. #endif
  101. WinMain(USE_INS hInst, USE_INS hPrevInstance,
  102.         USE_PTR szCmdLine,  int nCmdShow)
  103. {WNDCLASS  wc;
  104.  MSG msg;
  105.  BOOL Result;
  106.  if(!hPrevInstance)
  107.    {/* register main window class */
  108.     wc.style = CS_HREDRAW | CS_VREDRAW;
  109.     wc.lpfnWndProc = MainWndProc;
  110.     wc.cbClsExtra = 0;
  111.     wc.cbWndExtra = 0;
  112.     wc.hInstance = hInst;
  113.     wc.hIcon = LoadIcon(hInst, "HostIcon");
  114.     wc.hCursor = NULL;
  115.     wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  116.     wc.lpszMenuName =  "HostMenu";
  117.     wc.lpszClassName = "HostWClass";
  118.     Result = RegisterClass(&wc);
  119.     if(!Result) return FALSE;
  120.    }
  121.  /* create main window */
  122.  hInstance = hInst;
  123.  hMainWnd = CreateWindow(
  124.         "HostWClass",   "From",    WS_OVERLAPPEDWINDOW,
  125.         CW_USEDEFAULT,  CW_USEDEFAULT,
  126.         WinWidth,       WinHeight,
  127.         NULL,           NULL,
  128.         hInstance,      NULL);
  129.  ShowWindow(hMainWnd, nCmdShow);
  130.  UpdateWindow(hMainWnd);
  131.  
  132.  /* window control loop */
  133.  
  134.  while(GetMessage(&msg,NULL,0,0))
  135.    {
  136.     TranslateMessage(&msg);
  137.     DispatchMessage(&msg);
  138.    }
  139.  return (msg.wParam);
  140. } /* end WinMain */
  141.  
  142. #ifdef WIN32
  143. LRESULT CALLBACK
  144. #else
  145. long FAR PASCAL
  146. #endif
  147. MainWndProc(HWND hWindow,UINT iMsg,WPARAM wParam,LPARAM lParam)
  148. {int n, Code;
  149.  HDC hDC;
  150.  PAINTSTRUCT ps;
  151. #ifdef WIN32
  152. #else
  153.  static FARPROC lpfnAboutDlgProc;
  154. #endif
  155.  hMainWnd = hWindow;
  156.  switch (iMsg)
  157.     {case WM_CREATE:
  158.        /* create cursors */
  159.        ArrowCursor = LoadCursor(NULL, IDC_ARROW);
  160.        ///WaitCursor = LoadCursor(NULL, IDC_WAIT);
  161.        SetCursor(ArrowCursor);
  162. #ifdef WIN32
  163. #else
  164.         /* create thunk for Win16 */
  165.         lpfnAboutDlgProc = MakeProcInstance(AboutDlgProc,hInstance);
  166. #endif
  167.        /* initialize paint module */
  168.        PaintInit();
  169.        
  170.        /* define diagnostics log file */
  171.        ///seeStringParam(SEE_LOG_FILE, (LPSTR)"from.log"); 
  172.        /* turn off AUTO CALL driver */
  173.        seeIntegerParam(SEE_AUTO_CALL_DRIVER, 0); 
  174.        ShowINI();
  175.        /* verify that we have all strings read in */
  176.        if(lstrlen((LPSTR)Pop3String)==0) DisplayLine("ERROR: Missing POP3 server name.");
  177.        if(lstrlen((LPSTR)UserString)==0) DisplayLine("ERROR: Missing POP3 user name");
  178.        if(lstrlen((LPSTR)PassString)==0) DisplayLine("ERROR: Missing POP3 password");
  179.        break;
  180.  
  181.      case WM_COMMAND:
  182.          switch(wParam)
  183.            {
  184.             case MSG_ABOUT :
  185. #ifdef WIN32
  186.                DialogBox(hInstance, "AboutBox", hMainWnd, AboutDlgProc);
  187. #else
  188.                DialogBox(hInstance, "AboutBox", hMainWnd, lpfnAboutDlgProc);
  189. #endif
  190.                return 0;
  191.  
  192.             case MSG_DEBUG:
  193.               ShowINI();
  194.               break;
  195.  
  196.  
  197.             case MSG_BREAK:
  198.               NextCommand = CMD_NONE;
  199.               seeClose();
  200.               ///SetCursor(ArrowCursor);
  201.               break;
  202.  
  203.             case MSG_EXIT:
  204.               DestroyWindow(hMainWnd);
  205.               break;
  206.  
  207.             case MSG_MAIL_CHK:
  208.               /* set 1st command */
  209.               if(NextCommand==CMD_NONE) 
  210.                 {/* set 1st command in sequence */
  211.                  NextCommand = CMD_CONNECT; 
  212.                  POST_MSG(CMD_DRIVER);
  213.                 } 
  214.               break;
  215.            }
  216.          break;
  217.  
  218.     case WM_PAINT:
  219.       HideCaret(hMainWnd);
  220.       hDC = BeginPaint(hMainWnd, &ps);
  221.       SetMapMode(hDC,MM_ANISOTROPIC);
  222.       SelectObject(hDC, GetStockObject(OEM_FIXED_FONT) );
  223.       PaintMain(hDC,&ps);
  224.       EndPaint(hMainWnd,&ps);
  225.       SetCaretPos(PaintGetColPos(),PaintGetRowPos());
  226.       ShowCaret(hMainWnd);
  227.       break;
  228.       
  229.     case WM_USER:  /* posted by POST_MSG */
  230.       /* execute case */
  231.       switch(wParam)
  232.         {case CMD_DRIVER:
  233.            /* execute next driver state */
  234.            Code = seeDriver();
  235.            if(Code<0)
  236.              {DisplayError(Code,"seeDriver:");
  237.               break;
  238.              }
  239.            if(Code>0)
  240.              {POST_MSG(CMD_DRIVER);
  241.               break;    
  242.              }
  243.            /* Code==0 (driver is stopped): time to execute next command */  
  244.            POST_MSG(NextCommand);
  245.            break;
  246.         
  247.          case CMD_NONE:
  248.            break;
  249.            
  250.          case CMD_CONNECT:
  251.            /* connect to POP3 server */
  252.            DisplayLine((LPSTR)"Connecting...");
  253.            Code = seePop3Connect(
  254.                 (LPSTR)Pop3String,           /* POP3 server */
  255.                 (LPSTR)UserString,           /* user */ 
  256.                 (LPSTR)PassString);          /* password */
  257.            if(Code<0) 
  258.              {DisplayError(Code,"Connect:"); 
  259.               break;
  260.              }
  261.            /* next command after connecting */   
  262.            NextCommand = CMD_READY;
  263.            POST_MSG(CMD_DRIVER);
  264.            break;
  265.            
  266.          case CMD_READY: 
  267.            /* connected & ready to proceed */
  268.            DisplayLine((LPSTR)"Getting message count...");
  269.            Code = seeGetEmailCount();                   
  270.            if(Code<0)  
  271.              {DisplayError(Code,"Getting mail:");
  272.               NextCommand = CMD_NONE;
  273.               POST_MSG(CMD_DRIVER);
  274.               break;
  275.              } 
  276.            /* next command after requesting message count */
  277.            NextCommand = CMD_GET_COUNT;
  278.            POST_MSG(CMD_DRIVER);
  279.            break;
  280.            
  281.          case CMD_GET_COUNT:  
  282.            /* get # messages waiting */
  283.            MessageCount = seeStatistics(SEE_GET_MSG_COUNT);
  284.            wsprintf((LPSTR)Temp,"%d messages waiting.", MessageCount);
  285.            DisplayLine((LPSTR)Temp);
  286.            /* got any messages ? */
  287.            if(MessageCount==0)
  288.              {NextCommand = CMD_CLOSE;
  289.               POST_MSG(CMD_DRIVER);
  290.               break;
  291.              }
  292.            /* initialize for first email message */
  293.            MessageNbr = 0;
  294.            /* ready for first email message */
  295.            NextCommand = CMD_NEXT_MSG;
  296.            POST_MSG(CMD_DRIVER);
  297.            break;
  298.            
  299.          case CMD_NEXT_MSG:
  300.            /* any more email ? */
  301.            if(++MessageNbr>MessageCount)
  302.              {/* all messages have been read */
  303.               NextCommand = CMD_CLOSE;
  304.               POST_MSG(CMD_DRIVER);
  305.               break;
  306.              }
  307.            /* got more email */
  308.            wsprintf((LPSTR)Temp,"---[ Message %d ]------------------------------------",MessageNbr);
  309.            DisplayLine((LPSTR)Temp);
  310.            /* get headers from next email */
  311.            Code = seeGetEmailLines(MessageNbr, 0, (LPSTR)Buffer, BUFF_SIZE); 
  312.            if(Code<0)
  313.              {DisplayError(Code,"seeGetEmailLines:");
  314.               NextCommand = CMD_CLOSE;
  315.               POST_MSG(CMD_DRIVER);
  316.               break;
  317.              }
  318.            /* next command after requesting headers for this email message */    
  319.            NextCommand = CMD_GET_HEADERS;
  320.            POST_MSG(CMD_DRIVER);
  321.            break;
  322.                 
  323.          case CMD_GET_HEADERS:
  324.             /* display "DATE: " line */
  325.             n = seeExtractText((LPSTR)Buffer, "Date: ", (LPSTR)Temp, TEMP_SIZE);
  326.             if(n>0) 
  327.               {wsprintf((LPSTR)Temp,"%s", (LPSTR)Temp); 
  328.                DisplayString((LPSTR)Temp);
  329.               }
  330.             /* display "FROM: " line */
  331.             n = seeExtractText((LPSTR)Buffer, "From: ", (LPSTR)Temp, TEMP_SIZE);
  332.             if(n>0) 
  333.               {wsprintf((LPSTR)Temp,"%s", (LPSTR)Temp); 
  334.                DisplayString((LPSTR)Temp);
  335.               }
  336.             /* display "SUBJECT: " line */
  337.             n = seeExtractText((LPSTR)Buffer, "Subject: ", (LPSTR)Temp, TEMP_SIZE);
  338.             if(n>0) 
  339.               {wsprintf((LPSTR)Temp,"%s", (LPSTR)Temp);
  340.                DisplayString((LPSTR)Temp);
  341.               }
  342.            /* back for next email message */
  343.            NextCommand = CMD_NEXT_MSG;
  344.            POST_MSG(CMD_DRIVER);
  345.            break;
  346.            
  347.          case CMD_CLOSE: 
  348.            /* all done */
  349.            seeClose();  
  350.            if(MessageCount>0) 
  351.              DisplayLine((LPSTR)"-------------------------------------------------");
  352.            NextCommand = CMD_NONE;
  353.            POST_MSG(CMD_DRIVER);
  354.            break;
  355.         } /* end-switch(NextCommand) */
  356.       
  357.       break;
  358.       
  359.     case WM_DESTROY:
  360.       PostQuitMessage(0);
  361.       break;
  362.  
  363.     default:
  364.       return (DefWindowProc(hMainWnd, iMsg, wParam, lParam));
  365.    }
  366.   return 0;
  367.  }
  368.  
  369.